<?php
class WebTagModel extends Model {
	var $_table = 'web_tag';
	
	var $tag_id_nak; // INT UNSIGNED NOT NULL AUTO_INCREMENT,
	var $tag_sr; // VARCHAR(16) NOT NULL COMMENT '标签',
	var $type_sr = 'tag'; // ENUM('flag', 'tag') NOT NULL DEFAULT 'tag' COMMENT
	// '标签的类型',
	

	public function getFlags() {
		return $this->where ( array ('type' => 'flag' ) )->retrieve ( 'tag_id,tag' );
	}
	public function getTags() {
		return $this->where ( array ('type' => 'tag' ) )->retrieve ( 'tag_id,tag' );
	}
	/**
	 * 
	 * 根据tag列表生成带有tag_id的列表
	 * @param array $tags ('标签1','标签2'...)
	 * @return array (111=>'标签1’,'112'=>'标签2'...)
	 */
	public function getTagsInTags($tags) {
		$htags = $this->where ( array ('tag IN' => $tags, 'type' => 'tag' ) )->retrieve ( 'tag_id,tag' );
		if ($htags) {
			$htags = $htags->toArray ( 'tag_id', 'tag' );
		} else {
			$htags = array ();
		}
		$data = array ();
		foreach ( $tags as $tag ) {
			if (in_array ( $tag, $htags )) {
				continue;
			}
			$data [] = "('{$tag}')";
		}
		if (! empty ( $data )) {
			$fname = $this->fullname;
			$sql = "INSERT INTO `{$fname}` (tag) VALUES " . join ( ",", $data );
			$this->execute ( $sql );
			$htags = $this->where ( array ('tag IN' => $tags, 'type' => 'tag' ) )->retrieve ( 'tag_id,tag' );
			if ($htags) {
				$htags = $htags->toArray ( 'tag_id', 'tag' );
			} else {
				$htags = array ();
			}
		}
		return $htags;
	}
}